package com.neusoft.nep.controller;

import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class FeedbackDataListController {

    @FXML
    private TableView<FeedbackData> feedbackTableView;

    @FXML
    private TableColumn<FeedbackData, String> levelColumn;

    @FXML
    private TableColumn<FeedbackData, String> timeColumn;

    @FXML
    private TableColumn<FeedbackData, String> provinceColumn;

    @FXML
    private TableColumn<FeedbackData, String> cityColumn;

    @FXML
    public void initialize() {
        // 设置表格列的数据源
        levelColumn.setCellValueFactory(new PropertyValueFactory<>("level"));
        timeColumn.setCellValueFactory(new PropertyValueFactory<>("time"));
        provinceColumn.setCellValueFactory(new PropertyValueFactory<>("province"));
        cityColumn.setCellValueFactory(new PropertyValueFactory<>("city"));

        // 从文件加载反馈数据
        loadFeedbackDataFromFile();
    }

    private void loadFeedbackDataFromFile() {
        List<FeedbackData> feedbackDataList = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader("FeedbackData.txt"))) {
            String line;
            String currentRecord = "";

            while ((line = reader.readLine()) != null) {
                // 如果遇到分隔符，则处理当前记录
                if (line.contains("--------------------------------------------------")) {
                    if (!currentRecord.isEmpty()) {
                        FeedbackData data = parseFeedbackData(currentRecord);
                        if (data != null) {
                            feedbackDataList.add(data);
                        }
                        currentRecord = "";
                    }
                } else {
                    currentRecord += line + System.lineSeparator();
                }
            }

            // 处理文件最后一行数据
            if (!currentRecord.isEmpty()) {
                FeedbackData data = parseFeedbackData(currentRecord);
                if (data != null) {
                    feedbackDataList.add(data);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置表格数据
        feedbackTableView.getItems().addAll(feedbackDataList);

        // 设置等级列的单元格工厂，用于自定义等级显示（如背景颜色）
        levelColumn.setCellFactory(col -> new javafx.scene.control.TableCell<FeedbackData, String>() {
            @Override
            protected void updateItem(String level, boolean empty) {
                super.updateItem(level, empty);
                if (empty || level == null) {
                    setText(null);
                    setStyle("");
                } else {
                    setText(level);
                    // 根据等级设置不同的背景颜色
                    switch (level) {
                        case "一":
                            setStyle("-fx-background-color: #8BC34A;");
                            break;
                        case "二":
                            setStyle("-fx-background-color: #CDDC39;");
                            break;
                        case "三":
                            setStyle("-fx-background-color: #FFC107;");
                            break;
                        case "四":
                            setStyle("-fx-background-color: #FF9800;");
                            break;
                        case "五":
                            setStyle("-fx-background-color: #F44336;");
                            break;
                        case "六":
                            setStyle("-fx-background-color: #795548;");
                            break;
                        default:
                            setStyle("");
                    }
                }
            }
        });
    }

    private FeedbackData parseFeedbackData(String record) {
        String[] lines = record.split(System.lineSeparator());
        String time = "";
        String province = "";
        String city = "";
        String level = "";

        for (String line : lines) {
            if (line.startsWith("时间: ")) {
                time = line.substring("时间: ".length());
            } else if (line.startsWith("省份: ")) {
                province = line.substring("省份: ".length());
            } else if (line.startsWith("城市: ")) {
                city = line.substring("城市: ".length());
            } else if (line.startsWith("空气质量等级: ")) {
                level = line.substring("空气质量等级: ".length());
            }
        }

        // 检查是否解析到了必要信息
        if (!time.isEmpty() && !province.isEmpty() && !city.isEmpty() && !level.isEmpty()) {
            return new FeedbackData(level, time, province, city);
        }
        return null;
    }

    public static class FeedbackData {
        private final String level;
        private final String time;
        private final String province;
        private final String city;

        public FeedbackData(String level, String time, String province, String city) {
            this.level = level;
            this.time = time;
            this.province = province;
            this.city = city;
        }

        public String getLevel() {
            return level;
        }

        public String getTime() {
            return time;
        }

        public String getProvince() {
            return province;
        }

        public String getCity() {
            return city;
        }
    }
}
